import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;


	/**
	 * Constructor which creates an object of Recipe.
	 * @version 1.1
	 * @author Phat Le, Prince Kishore, Fahad Muzaffer
	 *
	 */
	public class Recipe {
		static Recipe[] recipeArray = new Recipe[6];

		String name;
		double price;
		int sugar;
		int milk;
		int coffee;
		int chocolate;
		int bouillon;
		int timesSold;
		static int i;
		
		/**
		 * Constructor for no parameters of Recipe.
		 */
		public Recipe () {
			this("", 0.0, 0, 0, 0, 0, 0);
		}
		
		/**
		 * Constructor with parameters.
		 * @param rec_name name of recipe
		 * @param rec_price price of recipe
		 * @param rec_sugar sugar for recipe
		 * @param rec_milk milk for recipe
		 * @param rec_coffee coffee for recipe
		 * @param rec_chocolate chocolate for recipe
		 * @param rec_bouillon bouillon for recipe
		 */
		public Recipe(String rec_name, double rec_price, int rec_sugar,
				int rec_milk, int rec_coffee, int rec_chocolate,
				int rec_bouillon) {
			this.name = rec_name;
			this.price = rec_price;
			this.sugar = rec_sugar;
			this.milk = rec_milk;
			this.coffee = rec_coffee;
			this.chocolate = rec_chocolate;
			this.bouillon = rec_bouillon;		
			timesSold = 0;
		}

		/**
		 * Extends the recipe array if more space is needed.
		 * Adds amount specified in addSpaceToRecipeArray. 
		 * @return amount of spaces to add. 
		 */
		private static int checkRecipes() {
			int numberRec = AcmeVendingMachine.addSpaceToRecipeArray;
			for (int i = 0; i < recipeArray.length; i++) {
				if (recipeArray[i].name.equalsIgnoreCase("")) {
					numberRec = i;
					break;
				}
			}
			return numberRec;
		}// end checkRecipes
		
		/**
		 * Adds a recipe.
		 * @throws NumberFormatException
		 */
		static void addRecipe() throws NumberFormatException {
			int empty = 0;
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	
			empty = checkRecipes(); // Checks amount of Space in recipeArray.
			if (empty == AcmeVendingMachine.addSpaceToRecipeArray) {
				System.out.println("Already 6 recipes made, no more!\n");
				AcmeVendingMachine.mainMenu();
			} else {
				i = empty;
			}
			// Variables to hold input
			String rec_name = "";
			String temp = ""; // holds name
			// int numtemp = 0; //error checking for integer input
			double rec_price = 0.0;
			int rec_sugar = 0;
			int rec_milk = 0;
			int rec_coffee = 0;
			int rec_chocolate = 0;
			int rec_bouillon = 0;
			try {
				System.out.println("\nEnter name of recipe:"); // Get name of recipe
																// to be added
				temp = br.readLine();
				if (temp.equalsIgnoreCase(null)) {
					System.out.println("Please enter a name next time, going back to Main Menu");
					AcmeVendingMachine.mainMenu();
				} else {
					rec_name = temp;
				}
				// get price
				System.out.println("Enter price of recipe, in decimal format (0.00):");
				System.out.print("$  ");
				if(AcmeVendingMachine.checkDouble(br.readLine(), AcmeVendingMachine.getMessage1(), AcmeVendingMachine.getMessage1()))
					rec_price = AcmeVendingMachine.getDoubleNumber();
	
				// get sugar
				System.out.println("Enter units of sugar:");
	
				if (AcmeVendingMachine.checkInt(br.readLine(), AcmeVendingMachine.getMessage1(), AcmeVendingMachine.getMessage1()))
					rec_sugar = AcmeVendingMachine.getIntNumber();
				// get milk
				System.out.println("Enter units of milk:");
				if (AcmeVendingMachine.checkInt(br.readLine(), AcmeVendingMachine.getMessage1(), AcmeVendingMachine.getMessage1()))
					rec_milk = AcmeVendingMachine.getIntNumber();
				// get coffee
				System.out.println("Enter units of coffee:");
				if (AcmeVendingMachine.checkInt(br.readLine(), AcmeVendingMachine.getMessage1(), AcmeVendingMachine.getMessage1()))
					rec_coffee = AcmeVendingMachine.getIntNumber();
				// get chocolate
				System.out.println("Enter units of chocolate:");
				if (AcmeVendingMachine.checkInt(br.readLine(), AcmeVendingMachine.getMessage1(), AcmeVendingMachine.getMessage1()))
					rec_chocolate = AcmeVendingMachine.getIntNumber();
				// get bouillon
				System.out.println("Enter units of bouillon:");
				if (AcmeVendingMachine.checkInt(br.readLine(), AcmeVendingMachine.getMessage1(), AcmeVendingMachine.getMessage1()))
					rec_bouillon = AcmeVendingMachine.getIntNumber();
	
				System.out.println("Name: " + rec_name + "\n  Price: " + rec_price
						+ "\n  Sugar: " + rec_sugar + "\n  Milk: " + rec_milk
						+ "\n  Coffee: " + rec_coffee + "\n  Chocolate: "
						+ rec_chocolate + "\n  Bouillon: " + rec_bouillon);
			} catch (IOException e) {
			}
			recipeArray[i] = new Recipe(rec_name, rec_price, rec_sugar, rec_milk,
					rec_coffee, rec_chocolate, rec_bouillon);
			i++;
	
			AcmeVendingMachine.mainMenu();
		}// end AddRecipe
		
		/**
		 * Deletes a recipe that is chosen from a menu after running this method.
		 * @throws NumberFormatException
		 */
		static void DeleteRecipe () throws NumberFormatException  {
			int num_select = 0;
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			
			System.out.println("Select which recipe to delete: ");
			for(int n = 0; n < recipeArray.length; n++){
				if(!recipeArray[n].name.equalsIgnoreCase("")){
					System.out.println(" " + n + ":  " + recipeArray[n].name);
				}
			}
			System.out.println(" 6:  Cancel\n");
			try{
				if(AcmeVendingMachine.checkInt(br.readLine(), AcmeVendingMachine.getMessage2(), AcmeVendingMachine.getMessage3()))
					num_select = AcmeVendingMachine.getIntNumber();
			} catch (IOException n){}
			
			if(num_select == 6){
				AcmeVendingMachine.mainMenu();
			}
			if(num_select >5){
				System.out.println("Sorry, please choose again.");
				DeleteRecipe();
			}
			if(recipeArray[num_select].name.equals("")){
				System.out.println("\nThere's no recipe to delete.\n");
				AcmeVendingMachine.mainMenu();
			}
			recipeArray[num_select] = new Recipe();
			System.out.println("");
			AcmeVendingMachine.mainMenu();
		}	
	}